package com.zhongxi.guoyuan.view.personalCenter;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ApplyRecordAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.ApplyRecordResult;
import com.zhongxi.guoyuan.entity.user.ApplyResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.MainActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WithDrawCashActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    private TextView tvWaitDeal;
    private TextView tvRecord;
    private int index = 1;
    private LinearLayout linearTitle;
    private String type;
    private PullToRefreshListView lvList;
    private ApplyRecordResult result;
    private ArrayList<ApplyRecordResult.ApplyRecordData> data = new ArrayList<>();
    private ApplyRecordAdapter adapter;
    private TextView tvTips;
    private TextView tvGoStore;
    private TextView tvTip;
    private TextView tvRest;
    private Map<String, String> map = new HashMap<>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_with_draw_cash);
        initTitle();
        tvTitle.setText("红包提现助手");
        tvWaitDeal = (TextView) findViewById(R.id.tvWaitDeal);
        tvRecord = (TextView) findViewById(R.id.tvRecord);
        tvTips = (TextView) findViewById(R.id.tvTips);
        tvTip = (TextView) findViewById(R.id.tvTip);
        tvRest = (TextView) findViewById(R.id.tvRest);
        tvGoStore = (TextView) findViewById(R.id.tvGoStore);
        linearTitle = (LinearLayout) findViewById(R.id.linearTitle);
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        tvWaitDeal.setSelected(true);
        tvRest.setText(MyApplication.getInstance().getRmb()+"元");
    }

    @Override
    protected void initData() {
        super.initData();
        getRecorder();
    }


    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tvRecord.setOnClickListener(this);
        tvWaitDeal.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        tvGoStore.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvWaitDeal:
                index = 1;
                linearTitle.setVisibility(View.GONE);
                tvWaitDeal.setSelected(true);
                tvRecord.setSelected(false);
                tvTip.setVisibility(View.VISIBLE);
                tvTips.setText("未提余额：");
                tvGoStore.setText("提现");
                getRecorder();
                break;
            case R.id.tvRecord:
                index = 2;
                linearTitle.setVisibility(View.VISIBLE);
                tvRecord.setSelected(true);
                tvWaitDeal.setSelected(false);
                tvTip.setVisibility(View.GONE);
                tvTips.setText("未提余额：");
                tvGoStore.setText("去逛逛");
                getRecorder();
                break;
            case R.id.tvGoStore:
                if(index == 1){
                    if(Float.parseFloat(MyApplication.getInstance().getRmb())>100){
                        showPopupWindow();
                    }else{
                        showShortToast("现金满100才可以提现");
                    }
                }else if(index == 2){
                    //去逛逛
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.putExtra("index",0);
                    startActivity(intent);
                }
                break;
        }
    }

    /**
     * 弹出提现提醒
     */
    private void showPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_apply, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvApply = (TextView) layout.findViewById(R.id.tvApply);
        TextView tvRest = (TextView) layout.findViewById(R.id.tvRest);
        TextView tvMoney = (TextView) layout.findViewById(R.id.tvMoney);
        ImageView ivCancel = (ImageView) layout.findViewById(R.id.ivCancel);
        //放弃支付
        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
//        final int money = Integer.parseInt(MyApplication.getInstance().getRmb());
        final int money = ((int)Float.parseFloat(MyApplication.getInstance().getRmb())/100)*100;
        tvMoney.setText(money+"元");
        float rest = Float.parseFloat(MyApplication.getInstance().getRmb())-money;
        tvRest.setText("剩余红包："+rest+"元");
        tvApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提现申请
                type = "apply";
                map.clear();
                map.put("amount", MyApplication.getInstance().descsc(money+""));
                getServer(Url.WAL_APPLY, map, "", false, null, "post");
                dialog.dismiss();
            }
        });
    }

    /**
     * 获取提现记录
     */
    private void getRecorder() {
        type = "recorder";
        String str = "&page="+pageNum+"&recodetype="+index;
        getServer(Url.APPLY_RECORD, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("recorder".equals(type)){
                result = gson.fromJson(response, ApplyRecordResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum == 1){
                    data.clear();
                }
                if(null!=result.getInfo()&&result.getInfo().size()>0){
                    data.addAll(result.getInfo());
                }
                adapter = new ApplyRecordAdapter(context, data, index);
                lvList.setAdapter(adapter);
                if(null!=result.getStoremphone()&&!"".equals(result.getStoremphone())){
                    adapter.setPhone(result.getStoremphone());
                }
                lvList.onRefreshComplete();
//                adapter.setIndex(index);
//                adapter.notifyDataSetChanged();
            }else if("apply".equals(type)){
                ApplyResult result = gson.fromJson(response, ApplyResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(null!=result.getBalance()&&!"".equals(result.getBalance()))
                    tvRest.setText(result.getBalance()+"元");
                getRecorder();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum=1;
        getRecorder();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getRecorder();
    }
}
